package com.bltec.emergency.controller.resController;

import com.alibaba.fastjson.JSON;
import com.bltec.emergency.common.Constant;
import com.bltec.emergency.common.ResponseJson;
import com.bltec.emergency.model.ResSupplies;
import com.bltec.emergency.service.resService.ResSuppliesService;
import com.bltec.emergency.service.mgrService.UserService;
import com.bltec.emergency.utils.DateUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/resSupplies")
@RequiresPermissions("project:res")
public class ResSuppliesController {
    @Resource
    private ResSuppliesService resSuppliesService;
    @Resource
    private UserService userService;
    /**
     * 获得所有
     */
    @RequestMapping("getAllResSupplies")
    public ResponseJson getTrain() {
        try {
            List<ResSupplies> ResSupplies = resSuppliesService.getAll();
            return new ResponseJson(Constant.OK, JSON.toJSON(ResSupplies));
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseJson(Constant.FAIL, e.getMessage());
        }
    }

    /**
     * 添加
     */
    @RequiresPermissions("res:insert")
    @RequestMapping("addResSupplies")
    public ResponseJson addTrain(String name, Long nums, Long departmentId, Long teamId, String category, String type,String function,String location,String manufacturer,Date productionTime,String maintainTime) {
        try {
            ResSupplies resSupplies = new ResSupplies();
            resSupplies.setName(name);
            resSupplies.setNums(nums);
            resSupplies.setDepartmentId(departmentId);
            resSupplies.setTeamId(teamId);
            resSupplies.setCategory(category);
            resSupplies.setType(type);
            resSupplies.setFunction(function);
            resSupplies.setLocation(location);
            resSupplies.setManufacturer(manufacturer);
            resSupplies.setProductionTime(productionTime);
            resSupplies.setMaintainTime(maintainTime);
            resSupplies.setEnable(true);
            resSupplies.setCreateBy(userService.getIdByUserName((String) SecurityUtils.getSubject().getPrincipal()));
            resSupplies.setCreateTime(DateUtil.dateNow());
            resSuppliesService.add(resSupplies);
            return new ResponseJson(Constant.OK, "创建用户成功");
        } catch (Exception e) {
            return new ResponseJson(Constant.FAIL, "创建用户失败" + e.getMessage());
        }
    }

    /**
     * 修改
     */
    @RequiresPermissions("res:update")
    @RequestMapping("updateResSupplies")
    public ResponseJson updateTrain(Long id, String name, Long nums, Long departmentId, Long teamId, String category, String type,String function,String location,String manufacturer,Date productionTime,String maintainTime) {
        try {
            ResSupplies resSupplies = resSuppliesService.getResSuppliesById(id);
            resSupplies.setName(name);
            resSupplies.setNums(nums);
            resSupplies.setDepartmentId(departmentId);
            resSupplies.setTeamId(teamId);
            resSupplies.setCategory(category);
            resSupplies.setType(type);
            resSupplies.setFunction(function);
            resSupplies.setLocation(location);
            resSupplies.setManufacturer(manufacturer);
            resSupplies.setProductionTime(productionTime);
            resSupplies.setMaintainTime(maintainTime);
            resSupplies.setUpdateBy(userService.getIdByUserName((String) SecurityUtils.getSubject().getPrincipal()));
            resSupplies.setUpdateTime(DateUtil.dateNow());
            resSuppliesService.updateResSuppliesById(resSupplies);
            return new ResponseJson(Constant.OK, JSON.toJSON(resSupplies));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseJson.UPDATE_ERROR;
        }
    }

    /**
     * 删除
     */
    @RequiresPermissions("res:delete")
    @RequestMapping("deleteResSupplies")
    public ResponseJson setDisable(Long id) {
        if (null != resSuppliesService.getResSuppliesById(id)) {
            if (resSuppliesService.deleteResSuppliesById(id)) {
                return new ResponseJson(Constant.OK, "删除成功");
            } else {
                return new ResponseJson(Constant.FAIL, "删除失败");
            }
        } else {
            return new ResponseJson(Constant.OK, "删除成功");
        }
    }
}
